package com.example.lll.lll_20_weather;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class WeatherParser {
    public static WeatherResponse parse(String jsonString) throws Exception {
        if (jsonString == null || jsonString.isEmpty()) {
            throw new Exception("空响应数据");
        }
        try {
            JSONObject json = new JSONObject(jsonString);
            WeatherResponse response = new WeatherResponse();

            if (!json.has("status")) {
                throw new Exception("缺少状态字段");
            }
            response.status = json.getInt("status");
            if (response.status != 200) {
                throw new Exception("API错误:" + json.optString("message", "未知错误"));
            }

            response.message = json.getString("message");
            response.time = json.optString("time", "N/A");

            // 解析城市列表
            if (json.has("cities")) {
                JSONArray citiesArray = json.getJSONArray("cities");
                List<WeatherResponse.CityWeather> cities = new ArrayList<>();

                for (int i = 0; i < citiesArray.length(); i++) {
                    JSONObject cityJson = citiesArray.getJSONObject(i);
                    WeatherResponse.CityWeather cityWeather = new WeatherResponse.CityWeather();

                    // 解析城市信息
                    cityWeather.cityInfo = parseCityInfo(cityJson.getJSONObject("cityInfo"));

                    // 解析天气数据
                    cityWeather.data = parseWeatherData(cityJson.getJSONObject("data"));

                    cities.add(cityWeather);
                }

                response.cities = cities;
            }

            return response;
        } catch (JSONException e) {
            throw new Exception("JSON解析错误：" + e.getMessage());
        }
    }

    private static WeatherResponse.CityInfo parseCityInfo(JSONObject json) throws Exception {
        WeatherResponse.CityInfo info = new WeatherResponse.CityInfo();
        info.city = json.optString("city", "未知城市");
        return info;
    }

    private static WeatherResponse.WeatherData parseWeatherData(JSONObject json) throws Exception {
        WeatherResponse.WeatherData data = new WeatherResponse.WeatherData();
        data.shidu = json.optString("shidu", "N/A");
        data.pm25 = json.optDouble("pm25", 0.0);
        data.wendu = json.optString("wendu", "N/A");
        data.forecast = parseForecast(json.getJSONArray("forecast"));
        return data;
    }

    private static List<WeatherResponse.Forecast> parseForecast(JSONArray array) throws Exception {
        List<WeatherResponse.Forecast> forecasts = new ArrayList<>();
        for (int i = 0; i < array.length(); i++) {
            JSONObject item = array.getJSONObject(i);
            WeatherResponse.Forecast forecast = new WeatherResponse.Forecast();
            forecast.ymd = item.optString("ymd","未知日期");
            forecast.type = item.optString("type", "未知天气");
            forecast.high = item.optString("high", "--");
            forecast.low = item.optString("low", "--");
            forecasts.add(forecast);
        }
        return forecasts;
    }
}
